home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer: Getting Started / Internet Surfer - Getting Started (Wayzata Technology)(7231)(1995).bin / pc / mac / bonus / peter_le / dehqx-20 / mymainlo.uni < prev    next >
Text File  |  1991-08-23  |  6KB  |  265 lines

  1. unit MyMainLoop;
  2. { DeHQX v2.0.0 ⌐ Peter Lewis, Aug 1991 }
  3.  
  4. interface
  5.  
  6.     uses
  7.         Types, OSUtils, Files, AppleTalk, Aliases, PPCToolBox, Processes, EPPC, Notification, AppleEvents, MyUtilities, MyTypes;
  8.  
  9.     type
  10.         HEreply = record
  11.                 todo: (T_Other, T_Menu, T_Close, T_Content, T_Key, T_AutoKey, T_Update, T_Dialog,{}
  12.                     T_MouseMoved, T_Suspend, T_Resume, T_Activate, T_Deactivate, T_HighLevel, T_Resize);
  13.                 themenu: integer;
  14.                 theitem: integer;
  15.                 HEwp: windowPtr;
  16.                 HEer: eventRecord;
  17.             end;
  18.  
  19.     procedure InitMainLoop;
  20.     procedure WaitForEvent (var er: eventRecord; sleep: longInt; rgn: rgnHandle);
  21.     function HandleEvents (er: eventRecord; var reply: HEreply): boolean;
  22.     procedure FinishMainLoop;
  23.  
  24. implementation
  25.  
  26.     procedure InitMainLoop;
  27.         var
  28.             dummy_er: eventRecord;
  29.             dummy: boolean;
  30.             i: integer;
  31.     begin
  32. { Give MultiFinder a chance to setup }
  33.         for i := 1 to 5 do
  34.             dummy := EventAvail(everyEvent, dummy_er);
  35.         InitUtilities;
  36.     end;
  37.  
  38.     procedure FinishMainLoop;
  39.     begin
  40.     end;
  41.  
  42.     procedure WaitForEvent (var er: eventRecord; sleep: longInt; rgn: rgnHandle);
  43.         const
  44.             max_sleep = 30;
  45.         var
  46.             b: boolean;
  47.             small_sleep: longInt;
  48.     begin
  49.         repeat
  50.             if max_sleep > sleep then
  51.                 small_sleep := sleep
  52.             else
  53.                 small_sleep := max_sleep;
  54.             if WaitGetNextEvent(everyEvent, er, small_sleep, nil) then
  55.                 leave;
  56.             b := IsDialogEvent(er);            { should be false, since GNE is }
  57.             sleep := sleep - small_sleep;
  58.         until sleep <= 0;
  59.     end;
  60.  
  61.     function HandleEvents (er: eventRecord; var reply: HEreply): boolean;
  62.         var
  63.             mResult: longInt;
  64.             tempRect, oldRect: rect;
  65.             myPt: point;
  66.             ch: char;
  67.             oe: OSErr;
  68.             code: integer;
  69.             good: boolean;
  70.             userWindow: boolean;
  71.     begin
  72.         reply.HEer := er;
  73.         good := false;
  74.         with reply do begin
  75.             todo := T_Other;
  76.             if IsDialogEvent(HEer) then
  77.                 if DialogSelect(HEer, HEwp, theitem) then begin
  78.                     todo := T_Dialog;
  79.                     good := true;
  80.                 end;
  81.             if HEer.what = MouseDown then
  82.                 code := FindWindow(HEer.where, HEwp)
  83.             else
  84.                 HEwp := FrontWindow;
  85.             userWindow := false;
  86.             if (HEwp <> nil) then
  87.                 userWindow := windowPeek(HEwp)^.windowKind >= userKind;
  88.             case HEer.what of
  89.                 MouseDown: 
  90.                     begin
  91.                     if code = inMenuBar then begin
  92.                         mResult := MenuSelect(HEer.where);
  93.                         if mResult <> 0 then begin
  94.                             themenu := HiWord(mResult);
  95.                             theitem := LoWord(mResult);
  96.                             todo := T_Menu;
  97.                             good := true;
  98.                         end;
  99.                     end;
  100.  
  101.                     if code = InDrag then begin
  102. {    tempRect := screenbits.bounds;}
  103. {    SetRect(tempRect, tempRect.Left + 10, tempRect.Top + 25, tempRect.Right - 10, tempRect.Bottom - 10);}
  104.                         tempRect := GetGrayRgn^^.rgnBBox;
  105.                         DragWindow(HEwp, HEer.where, tempRect);
  106.                     end;
  107.  
  108.                     if (code = inGrow) and (HEwp <> nil) then begin
  109.                         SetPort(HEwp);
  110.                         myPt := HEer.where;
  111.                         GlobalToLocal(myPt);
  112.                         OldRect := HEwp^.portRect;
  113.                         with screenbits.bounds do
  114.                             SetRect(tempRect, 15, 15, (right - left), (bottom - top) - 20);
  115.                         mResult := GrowWindow(HEwp, HEer.where, tempRect);
  116.                         SizeWindow(HEwp, LoWord(mResult), HiWord(mResult), TRUE);
  117.                         SetPort(HEwp);
  118.                         SetRect(tempRect, 0, myPt.v - 15, myPt.h + 15, myPt.v + 15);
  119.                         EraseRect(tempRect);
  120.                         InvalRect(tempRect);
  121.                         SetRect(tempRect, myPt.h - 15, 0, myPt.h + 15, myPt.v + 15);
  122.                         EraseRect(tempRect);
  123.                         InvalRect(tempRect);
  124.                         todo := T_Resize;
  125.                         good := true;
  126.                     end;
  127.  
  128.                     if (code = inZoomIn) or (code = inZoomOut) then begin
  129.                         if HEwp <> nil then begin
  130.                             SetPort(HEwp);
  131.                             myPt := HEer.where;
  132.                             GlobalToLocal(myPt);
  133.                             OldRect := HEwp^.portRect;
  134.                             if TrackBox(HEwp, myPt, code) then begin
  135.                                 ZoomWindow(HEwp, code, true);
  136.                                 SetRect(tempRect, 0, 0, 32000, 32000);
  137.                                 EraseRect(tempRect);
  138.                                 InvalRect(tempRect);
  139.                                 todo := T_Resize;
  140.                                 good := true;
  141.                             end;
  142.                         end;
  143.                     end;
  144.  
  145.                     if code = inGoAway then begin
  146.                         if HEwp <> FrontWindow then
  147.                             SelectWindow(HEwp)
  148.                         else if TrackGoAway(HEwp, HEer.where) then begin
  149.                             if SimpleClose(HEwp) then begin
  150.                                 good := true;
  151.                                 todo := T_Close;
  152.                             end;
  153.                         end;
  154.                     end;
  155.  
  156.                     if code = inContent then begin
  157.                         if HEwp <> FrontWindow then
  158.                             SelectWindow(HEwp)
  159.                         else begin
  160.                             if userWindow then begin
  161.                                 good := true;
  162.                                 todo := T_Content;
  163.                                 GlobalToLocal(HEer.where);
  164.                             end;
  165.                         end;
  166.                     end;
  167.  
  168.                     if (code = inSysWindow) then
  169.                         SystemClick(HEer, HEwp);
  170.  
  171.                 end;
  172.  
  173.                 AutoKey: 
  174.                     begin
  175.                     if not Odd(HEer.modifiers div CmdKey) then begin
  176.                         good := true;
  177.                         todo := T_AutoKey;
  178.                     end;
  179.                 end;
  180.  
  181.                 KeyDown: 
  182.                     begin
  183.                     with HEer do begin
  184.                         ch := chr(BAND(message, CharCodeMask));
  185.                         todo := T_Key;
  186.                         if Odd(modifiers div CmdKey) then begin
  187.                             mResult := MenuKey(ch);
  188.                             if mResult <> 0 then begin
  189.                                 themenu := HiWord(mResult);
  190.                                 theitem := LoWord(mResult);
  191.                                 todo := T_Menu;
  192.                             end;
  193.                         end;
  194.                         good := true;
  195.                     end;
  196.                 end;
  197.  
  198.                 UpdateEvt: 
  199.                     begin
  200.                     HEwp := windowPtr(HEer.message);
  201.                     good := true;
  202.                     todo := T_Update;
  203.                 end;
  204.  
  205.                 DiskEvt: 
  206.                     begin
  207.                     if (HiWord(HEer.message) <> noErr) then begin
  208.                         HEer.where.h := ((screenbits.bounds.Right - screenbits.bounds.Left) div 2) - (304 div 2);
  209.                         HEer.where.v := ((screenbits.bounds.Bottom - screenbits.bounds.Top) div 3) - (104 div 2);
  210.                         InitCursor;
  211.                         oe := DIBadMount(HEer.where, HEer.message);
  212.                     end;
  213.                 end;
  214.  
  215.                 ActivateEvt: 
  216.                     begin
  217.                     HEwp := WindowPtr(HEer.message);
  218.                     if odd(HEer.modifiers) then begin
  219.                         SelectWindow(HEwp);
  220.                         todo := T_Activate;
  221.                     end
  222.                     else begin
  223.                         todo := T_Deactivate;
  224.                     end;
  225.                     good := true;
  226.                 end;
  227.  
  228.                 kOSEvent: 
  229.                     case BAnd(BRotL(HEer.message, 8), $FF) of    {high byte of message}
  230.                         kMouseMovedMessage: 
  231.                             begin
  232.                             todo := T_MouseMoved;
  233.                             good := true;
  234.                         end;
  235.                         kSuspendResumeMessage: 
  236.                             begin
  237.                             in_foreground := BAnd(HEer.message, kResumeMask) <> 0;
  238.                             InitCursor;
  239.                             if in_foreground then begin
  240.                                 todo := T_Resume;
  241.                             end
  242.                             else
  243.                                 todo := T_Suspend;
  244.                             good := true;
  245.                         end;
  246.                         otherwise
  247.                             ;
  248.                     end;
  249.  
  250.                 kHighLevelEvent: 
  251.                     begin
  252.                     todo := T_HighLevel;
  253.                     good := true;
  254.                     if has_AppleEvents then
  255.                         oe := AEProcessAppleEvent(HEer);
  256.                 end;
  257.  
  258.                 otherwise
  259.             end;
  260.  
  261.             HandleEvents := good;
  262.         end;
  263.     end;
  264.  
  265. end.